<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <!-- 搜索模块 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item prop="jobName">
          <el-input v-model="queryParams.jobName" placeholder="请输入任务名称" clearable style="width: 240px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item prop="jobGroup">
          <el-select v-model="queryParams.jobGroup" placeholder="请选择任务组名" clearable style="width: 240px">
            <el-option v-for="dict in dict.type.sys_job_group" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择执行状态" clearable style="width: 240px">
            <el-option v-for="dict in dict.type.sys_common_status" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button class="btn_primary" type="primary" icon="el-icon-search" size="mini"
            @click="handleQuery">搜索</el-button>
          <el-button class="btn_cancel" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 搜索模块over -->
      <el-row class="marginb-15">
        <el-col :span="2">
        </el-col>
      </el-row>
      <!-- 表格模块 -->
      <div class="table_container">
        <el-table v-loading="loading" :data="jobLogList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="日志编号" width="80" align="center" prop="jobLogId" />
          <el-table-column label="任务名称" align="center" prop="jobName" :show-overflow-tooltip="true" />
          <el-table-column label="任务组名" align="center" prop="jobGroup" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_job_group" :value="scope.row.jobGroup" />
            </template>
          </el-table-column>
          <el-table-column label="调用目标字符串" align="center" prop="invokeTarget" :show-overflow-tooltip="true" />
          <el-table-column label="日志信息" align="center" prop="jobMessage" :show-overflow-tooltip="true" />
          <el-table-column label="执行状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="执行时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
                v-hasPermi="['monitor:job:query']">详细</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @change="getList"></pagination>
      <!-- 分页over -->

      <el-dialog title="日志详情" :visible.sync="dialogVisibleInfo" width="800" append-to-body>
            <BaseDescriptions :descConfig="descConfig" :descConfigItem="descConfigItem" :descModel="form">
              <template #jobGroup>
                    <dict-tag :options="dict.type.sys_job_group" :value="form.jobGroup" />
                </template>
                <template #status>
                    <dict-tag :options="dict.type.sys_common_status" :value="form.status" />
                </template>
                <template #createTime>
                  {{ parseTime(form.createTime) }}
                </template>
            </BaseDescriptions>
        </el-dialog>

    </div>
  </div>
</template>

<script>
import { getJob } from "@/api/monitor/job";
import { listJobLog, delJobLog, cleanJobLog } from "@/api/monitor/jobLog";

import BaseDescriptions from '@/components/PageContent/BaseDescriptions.vue';

export default {
  name: 'Job',
  dicts: ['sys_job_group', 'sys_job_status'],
  components: {
    BaseDescriptions,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调度日志表格数据
      jobLogList: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jobName: undefined,
        jobGroup: undefined,
        status: undefined
      },
      dialogVisibleInfo:false,
      descConfig: {
        size: 'medium',
        column: '2',
      },
      descConfigItem: [
        {
          fieIdName: 'jobLogId',
          label: '日志编号',
          span: 2
        },
        {
          fieIdName: 'jobName',
          label: '任务名称',
          span: 2
        },
        {
          fieIdName: 'invokeTarget',
          label: '调用目标字符串',
          span: 2
        },
        {
          fieIdName: 'jobMessage',
          label: '日志信息',
          span: 2
        },
        {
          fieIdName: 'jobGroup',
          label: '日志信息',
          span: 2,
          slotName: 'jobGroup',
        },
        {
          fieIdName: 'status',
          label: '执行状态',
          span: 2,
          slotName: 'status',
        },
        {
          fieIdName: 'createTime',
          label: '执行时间',
          span: 2,
          slotName: 'createTime',
        },
      ]
    };
  },
  computed: {
    pageTitle() {
      return '日志列表';
    },
  },
  created() {
    const jobId = this.$route.query && this.$route.query.jobId;
    if (jobId !== undefined && jobId != 0) {
      getJob(jobId).then(response => {
        this.queryParams.jobName = response.data.jobName;
        this.queryParams.jobGroup = response.data.jobGroup;
        this.getList();
      });
    } else {
      this.getList();
    }
  },
  mounted() { },
  watch: {},
  methods: {
    /** 查询定时任务列表 */
    getList() {
      this.loading = true;
      listJobLog(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.jobLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    // 任务组名字典翻译
    jobGroupFormat(row, column) {
      return this.selectDictLabel(this.dict.type.sys_job_group, row.jobGroup);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jobId: undefined,
        jobName: undefined,
        jobGroup: undefined,
        invokeTarget: undefined,
        cronExpression: undefined,
        misfirePolicy: 1,
        concurrent: 1,
        status: '0',
        remark: undefined,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.searchMap.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.jobId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case 'handleRun':
          this.handleRun(row);
          break;
        case 'handleView':
          this.handleView(row);
          break;
        case 'handleJobLog':
          this.handleJobLog(row);
          break;
        default:
          break;
      }
    },
    // 任务状态修改
    handleStatusChange(row) {
      let text = row.status === '0' ? '启用' : '停用';
      this.$confirm('确认要"' + text + '""' + row.jobName + '"任务吗？')
        .then(function () {
          return changeJobStatus(row.jobId, row.status);
        })
        .then(() => {
          this.$message.success('执行成功');
        })
        .catch(function () {
          row.status = row.status === '0' ? '1' : '0';
        });
    },
    /* 立即执行一次 */
    handleRun(row) {
      this.$confirm('确认要立即执行一次"' + row.jobName + '"任务吗？')
        .then(function () {
          return runJob(row.jobId, row.jobGroup);
        })
        .then(() => {
          this.$message.success('执行成功');
        })
        .catch(() => { });
    },
    /** 任务详细信息 */
    handleView(row) {
      this.form = row;
      this.dialogVisibleInfo = true;
      // getJob(row.jobId).then((res) => {
      //   this.form = res.data;
      //   this.openView = true;
      // });
    },
    /** cron表达式按钮操作 */
    handleShowCron() {
      this.expression = this.form.cronExpression;
      this.openCron = true;
    },
    /** 确定后回传值 */
    crontabFill(value) {
      this.form.cronExpression = value;
    },
    /** 任务日志列表查询 */
    handleJobLog(row) {
      const jobId = row.jobId || 0;
      this.$router.push('timeJob/log' + jobId);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加任务';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const jobId = row.jobId || this.ids;
      getJob(jobId).then((res) => {
        this.form = res.data;
        this.open = true;
        this.title = '修改任务';
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.jobId != undefined) {
            updateJob(this.form).then((response) => {
              this.$message.success('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addJob(this.form).then((response) => {
              this.$message.success('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jobIds = row.jobId || this.ids;
      this.$confirm('是否确认删除定时任务编号为"' + jobIds + '"的数据项？')
        .then(function () {
          return delJob(jobIds);
        })
        .then(() => {
          this.getList();
          this.$message.success('删除成功');
        })
        .catch(() => { });
    }
  }
};
</script>

<style lang="less" scoped></style>
